Skip to content

fix(spend): remove duplicate _common_add_spend_log_transaction call in agent spend path#21288

Closed
ArivunidhiA wants to merge 1 commit intoBerriAI:mainfrom
ArivunidhiA:fix/duplicate-spend-log-agent-21181
Closed

fix(spend): remove duplicate _common_add_spend_log_transaction call in agent spend path#21288
ArivunidhiA wants to merge 1 commit intoBerriAI:mainfrom
ArivunidhiA:fix/duplicate-spend-log-agent-21181

Conversation

@ArivunidhiA
Copy link

Fixes #21181

add_spend_log_transaction_to_daily_agent_transaction() calls _common_add_spend_log_transaction_to_daily_transaction() twice for the same request when agent_id is present. The second call uses payload_with_agent_id, which is just {**payload, "agent_id": payload["agent_id"]} — identical to payload since agent_id is already present. This duplicates JSON metadata parsing and status computation work on every agent spend log.

Fix: Remove the redundant second call and the unnecessary payload_with_agent_id construction. Reuse base_daily_transaction from the first call, matching the pattern already used by the team, user, org, and end_user variants of this function.

Changed files:

  • litellm/proxy/db/db_spend_update_writer.py — removed 13 lines of redundant code
  • tests/test_litellm/proxy/db/test_db_spend_update_writer.py — added regression test verifying the common helper is called exactly once

@vercel
Copy link

vercel bot commented Feb 16, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Feb 16, 2026 3:03am

Request Review

@CLAassistant
Copy link

CLAassistant commented Feb 16, 2026

CLA assistant check
All committers have signed the CLA.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 16, 2026

Greptile Summary

This PR removes a redundant duplicate call to _common_add_spend_log_transaction_to_daily_transaction in the agent spend logging path. The original code called this helper twice with effectively identical payloads (payload_with_agent_id was {**payload, "agent_id": payload["agent_id"]}, which is identical to payload since agent_id is already present). This doubled the JSON metadata parsing and status computation work on every agent spend log.

  • Removed the unnecessary payload_with_agent_id construction and the second _common_add_spend_log_transaction_to_daily_transaction call in add_spend_log_transaction_to_daily_agent_transaction
  • The method now reuses base_daily_transaction from the first call, matching the pattern used by team, user, tag, and other variants
  • Added a regression test verifying the common helper is called exactly once per request (mock-only, no network calls)
  • Net deletion of 13 lines of redundant code with no behavioral change

Confidence Score: 5/5

  • This PR is safe to merge — it removes provably redundant code with no behavioral change and includes a regression test.
  • The change is minimal and clearly correct. The removed code was a redundant second call to the same helper with an identical payload. The fix aligns the agent method with the established pattern used by all other entity-type variants (team, user, tag, end_user). The regression test validates the fix. No new logic is introduced.
  • No files require special attention.

Important Files Changed

Filename Overview
litellm/proxy/db/db_spend_update_writer.py Removed 13 lines of redundant code: the duplicate _common_add_spend_log_transaction_to_daily_transaction call and the unnecessary payload_with_agent_id construction. The method now follows the same single-call pattern used by team, user, and tag variants.
tests/test_litellm/proxy/db/test_db_spend_update_writer.py Added regression test for #21181 verifying _common_add_spend_log_transaction_to_daily_transaction is called exactly once in the agent spend path. Test uses mocks only (no network calls), following the project's test conventions.

Sequence Diagram

sequenceDiagram
    participant Caller as Spend Logger
    participant Agent as add_spend_log_transaction_to_daily_agent_transaction
    participant Common as _common_add_spend_log_transaction_to_daily_transaction
    participant Queue as daily_agent_spend_update_queue

    Note over Agent: Before Fix (2 calls)
    Caller->>Agent: payload (with agent_id)
    Agent->>Common: payload, "agent" (1st call)
    Common-->>Agent: base_daily_transaction
    Agent->>Agent: construct payload_with_agent_id (redundant copy)
    Agent->>Common: payload_with_agent_id, "agent" (2nd call - DUPLICATE)
    Common-->>Agent: base_daily_transaction (same result)
    Agent->>Queue: add_update(daily_transaction)

    Note over Agent: After Fix (1 call)
    Caller->>Agent: payload (with agent_id)
    Agent->>Common: payload, "agent" (single call)
    Common-->>Agent: base_daily_transaction
    Agent->>Queue: add_update(daily_transaction)
Loading

Last reviewed commit: 87ba840

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@ArivunidhiA
Copy link
Author

The 6 failing "LiteLLM Unit Tests (Matrix)" checks (integrations, llms, proxy-guardrails, proxy-unit-a, proxy-unit-b, root) are pre-existing failures that affect all fork PRs due to missing repo secrets. These same tests fail on unrelated docs-only PRs (e.g., #21283, #21282).

All checks that can run on fork PRs pass successfully.

@krrishdholakia
Copy link
Member

Fixed with #21187

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance: duplicate _common_add_spend_log_transaction call in daily agent spend path

3 participants